package com.controller;

import com.entity.Component;
import com.service.ComponentService;
import com.service.ImportTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author XiaoHao
 * @date 2021-03-19 17:14
 */
@Controller
@RequestMapping("/con")
public class ComponentController {

    @Autowired
    ImportTable importTable;

    @RequestMapping("test")
    @ResponseBody
    public String test(HttpServletRequest request) {
        try {
            Component oldT = new Component();
            Component newT = new Component();
            int i = 1;

            HashMap<String, String> hashMap = new HashMap<>();
            while (true) {

                String old1 = request.getParameter("old" + i);
                String new1 = request.getParameter("new" + i);
                i++;
                if (old1 != null && new1 != null) {
                    hashMap.put(old1, new1);
                } else {
                    System.out.println("没有取的值,结束循环");
                    break;
                }
            }
            String oldTable = request.getParameter("oldTable");
            String newTable = request.getParameter("newTable");

            oldT.setTableName(oldTable);
            newT.setTableName(newTable);
            System.out.println("老表名" + oldT + "新表名" + newT + "获取到的值=====" + hashMap);

            importTable.importTable(hashMap, oldT, newT);

        } catch (Exception e) {
            return "导入失败!";
        }

        return "导入成功";
    }
}
